﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using GPS.GPS_DAL;

namespace GPS.Controllers.GPS_NghiepVu
{
    public class Select2OptionModel
    {
        public string id { get; set; }

        public string text { get; set; }
    }

    public class Select2PagedResult
    {
        public int Total { get; set; }

        public List<Select2OptionModel> Results { get; set; }
    }

    public class GPS_AjaxController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        //
        // GET: /GPS_Ajax/
        public ActionResult Index()
        {
            return View();
        }


        

        public JsonResult Select2KH_MA_BCI(string searchTerm, string dvgtvt_ma)
        {
            string _madvgtvt = dvgtvt_ma;
            if (string.IsNullOrWhiteSpace(dvgtvt_ma))
            {
                ApplicationUser curUser = getCurrentUser();
                _madvgtvt = curUser.MA_TINHTP;

            }
            var result = new Select2PagedResult();
            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                string _ma_DVGT = _madvgtvt;
                var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_BCI(_ma_DVGT, searchTerm).Take(20).ToList();
                result.Results = new List<Select2OptionModel>();
                khoahocs.ForEach(o =>
                {
                    result.Results.Add(new Select2OptionModel() { id = o.KH_MA, text = o.KH_TEN });
                });
            }

            return Json(result, JsonRequestBehavior.AllowGet);

            // return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Select2KH_MA(string searchTerm, string dvgtvt_ma)
        {
            string _madvgtvt = dvgtvt_ma;
            if (string.IsNullOrWhiteSpace(dvgtvt_ma))
            {
                ApplicationUser curUser = getCurrentUser();
                _madvgtvt = curUser.MA_TINHTP;

            }
            var result = new Select2PagedResult();
            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                string _ma_DVGT = _madvgtvt;
                var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN1(_ma_DVGT, searchTerm).Take(20).ToList();
                result.Results = new List<Select2OptionModel>();
                khoahocs.ForEach(o =>
                {
                    result.Results.Add(new Select2OptionModel() { id = o.KH_MA, text = o.KH_TEN });
                });
            }

            return Json(result, JsonRequestBehavior.AllowGet);

            // return Json(result, JsonRequestBehavior.AllowGet);
        }



        public JsonResult Select2KH_MA_TN(string searchTerm, string dvgtvt_ma)
        {
            string _madvgtvt = dvgtvt_ma;
            if (string.IsNullOrWhiteSpace(dvgtvt_ma))
            {
                ApplicationUser curUser = getCurrentUser();
                _madvgtvt = curUser.MA_TINHTP;

            }
            var result = new Select2PagedResult();
            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                string _ma_DVGT = _madvgtvt;
                var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_TN(_ma_DVGT, searchTerm).ToList();
                result.Results = new List<Select2OptionModel>();
                khoahocs.ForEach(o =>
                {
                    result.Results.Add(new Select2OptionModel() { id = o.KH_MA, text = o.KH_TEN });
                });
            }

            return Json(result, JsonRequestBehavior.AllowGet);

            // return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Select2KH_MA_Search(string searchTerm, string dvgtvt_ma)
        {
            string _madvgtvt = dvgtvt_ma;
            if (string.IsNullOrWhiteSpace(dvgtvt_ma))
            {
                ApplicationUser curUser = getCurrentUser();
                _madvgtvt = curUser.MA_TINHTP;

            }
            var result = new Select2PagedResult();
            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                string _ma_DVGT = _madvgtvt;
                var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN1(_ma_DVGT, searchTerm).Take(20).ToList();
                result.Results = new List<Select2OptionModel>();
                khoahocs.ForEach(o =>
                {
                    result.Results.Add(new Select2OptionModel() { id = o.KH_MA, text = o.KH_TEN });
                });
            }

            return Json(result, JsonRequestBehavior.AllowGet);

            // return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Select2KH_MA_LH(string searchTerm, string dvgtvt_ma)
        {
            string _madvgtvt = dvgtvt_ma;
            if (string.IsNullOrWhiteSpace(dvgtvt_ma))
            {
                ApplicationUser curUser = getCurrentUser();
                _madvgtvt = curUser.MA_TINHTP;

            }

            var result = new Select2PagedResult();
            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                string _ma_DVGT = _madvgtvt;
                var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH1(_ma_DVGT, searchTerm);
                result.Results = new List<Select2OptionModel>();
                khoahocs.ForEach(o =>
                {
                    result.Results.Add(new Select2OptionModel() { id = o.KH_MA, text = o.KH_TEN });
                });
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Select2KH_MA_LH_Search(string searchTerm, string dvgtvt_ma)
        {
            string _madvgtvt = dvgtvt_ma;
            //if (string.IsNullOrWhiteSpace(dvgtvt_ma))
            //{
            //    ApplicationUser curUser = getCurrentUser();
            //    _madvgtvt = curUser.MA_TINHTP;

            //}

            var result = new Select2PagedResult();
            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                string _ma_DVGT = _madvgtvt;
                var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH1(_ma_DVGT, searchTerm);
                result.Results = new List<Select2OptionModel>();
                khoahocs.ForEach(o =>
                {
                    result.Results.Add(new Select2OptionModel() { id = o.KH_MA, text = o.KH_TEN });
                });
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Select2KH_MA_LH_TraCuu(string searchTerm, string dvgtvt_ma)
        {
            string _madvgtvt = dvgtvt_ma;
            //if (string.IsNullOrWhiteSpace(dvgtvt_ma))
            //{
            //    ApplicationUser curUser = getCurrentUser();
            //    _madvgtvt = curUser.MA_TINHTP;

            //}

            var result = new Select2PagedResult();
            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                string _ma_DVGT = _madvgtvt;
                var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH_TC(_ma_DVGT, searchTerm);
                result.Results = new List<Select2OptionModel>();
                khoahocs.ForEach(o =>
                {
                    result.Results.Add(new Select2OptionModel() { id = o.KH_MA, text = o.KH_TEN });
                });
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Select2_DVGTVT_Search(string searchTerm)
        {
            var result = new Select2PagedResult();
            if (!string.IsNullOrWhiteSpace(searchTerm))
            {

                var models = _db.Filter<SDONVIGTVT>(o => (o.DVGTVT_MA.Contains(searchTerm) || o.DVGTVT_TEN.ToUpper().Contains(searchTerm.ToUpper())) && o.DVGTVT_ACTIVE=="1" && o.DVGTVT_MAQL=="40");
                result.Results = new List<Select2OptionModel>();
                models.ForEach(o =>
                {
                    result.Results.Add(new Select2OptionModel() { id = o.DVGTVT_MA, text = o.DVGTVT_TEN });
                });
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Select2KH_MA_TraCuu(string searchTerm, string dvgtvt_ma)
        {
            string _madvgtvt = dvgtvt_ma;
            if (string.IsNullOrWhiteSpace(dvgtvt_ma))
            {
                ApplicationUser curUser = getCurrentUser();
                _madvgtvt = curUser.MA_TINHTP;

            }
            var result = new Select2PagedResult();
            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                string _ma_DVGT = _madvgtvt;
                var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_TC(_ma_DVGT, searchTerm).Take(20).ToList();
                result.Results = new List<Select2OptionModel>();
                khoahocs.ForEach(o =>
                {
                    result.Results.Add(new Select2OptionModel() { id = o.KH_MA, text = o.KH_TEN });
                });
            }

            return Json(result, JsonRequestBehavior.AllowGet);

            // return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}